//
//  UIImage+extension.swift
//  ERecycle-Shop
//
//  Created by abb on 16/8/13.
//  Copyright © 2016年 abb. All rights reserved.
//

import Foundation

extension UIImage {
    func rotatedByRadians(radians:CGFloat) -> UIImage {
        let rotatedViewBox = UIView(frame: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        let t = CGAffineTransformMakeRotation(radians)
        rotatedViewBox.transform = t
        let rotatedSize = rotatedViewBox.frame.size
        
        
        UIGraphicsBeginImageContext(rotatedSize)
        let bitmap = UIGraphicsGetCurrentContext()
        CGContextTranslateCTM(bitmap, rotatedSize.width / 2, rotatedSize.height / 2)
        
        CGContextRotateCTM(bitmap, radians)
        
        CGContextScaleCTM(bitmap, 1.0, -1.0)
        CGContextDrawImage(bitmap, CGRect(x: -self.size.width / 2, y: -self.size.height / 2, width: self.size.width, height: self.size.height), self.CGImage)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage
    }
    
    class func imageWithColor(color:UIColor,size:CGSize)-> UIImage {
        let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        CGContextSetFillColorWithColor(context, color.CGColor)
        CGContextFillRect(context, rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        return image
    }
}
func DegreesToRadians(degrees:CGFloat) -> CGFloat {
    return degrees * CGFloat(M_PI) / 180
}